home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / kshfuncs / bang next >
Encoding:
Text File  |  1997-08-26  |  1.7 KB  |  51 lines

  1. # @(#) bang.ksh 1.0 92/12/23
  2. # 92/12/23 john h. dubois iii (john@armory.com)
  3. # Syntax:
  4. # bang <hist-sel>    Prints the command that matches the history selector.
  5. # bang :<arg-sel>    Prints the selected args from the last command.
  6. # bang <hist-sel:<arg-sel>    Prints the selected args from the command
  7. #         that matches the history selector.
  8. # An optional : may follow the first form.
  9. # <hist-sel> format:
  10. # n    Selects command number n from the history list.
  11. # -n    Selects the n'th-to-last command from the history list.
  12. # s    Selects the last command that has string s in it.
  13. # <arg-sel> format:
  14. # The arguments are numbered starting with 0, where argument 0 is the
  15. # command, argument 1 is the first argument to the command, etc.
  16. # n    Selects argument number n
  17. # n-
  18. # n-$    Both forms select arguments n through the last argument.
  19. # $    Selects the last argument.
  20. # -m    Selects arguments 1 through m.
  21. # *    Selects arguments 1 through the last argument.
  22. # Typical usage:
  23. # alias !=bang
  24. # foo `! $`    Runs foo with the last argument from the last command.
  25. function bang {
  26.     if [ $# != 1 ]; then
  27.     print -u2 "Wrong number of arguments."
  28.     return 1
  29.     fi
  30.     typeset sel args min max
  31.     sel=${1%%:*}
  32.     [[ $1 = $sel?(:) ]] && args=0-1024 || args=${1#*:}
  33.     [ -z "$sel" ] && sel=-1
  34.     min=${args%-*} max=${args#*-}
  35.     set -- `fc -nl $sel $sel`
  36.     [ "$max" = \$ -o -z "$max" -o "$max" = \* ] && let max=$#-1
  37.     [ -z "$min" -o "$min" = \* ] && min=1
  38.     [ "$min" = \$ ] && let min=$#-1
  39.     [ min -gt $# ] && min=$#
  40.     shift $min
  41.     let max=max-min+1
  42.     [ max -gt $# ] && max=$#
  43.     while [ max -gt 0 ]; do
  44.     print -n -- "$1 "
  45.     let max=max-1
  46.     [ $# -gt 0 ] && shift
  47.     done
  48.     print ""
  49. }
  50.  
  51.